<?php defined('SYSPATH') or die('No direct access allowed.');

class Model_User extends Model_Auth_User {
    
        protected $_has_many = array(
		'user_tokens'       => array('model' => 'user_token'),
		'roles'             => array('model' => 'role', 'through' => 'roles_users'),
                'fields'            => array('model' => 'field', 'through' => 'fields_users'),
                'inbox'             => array('model' => 'inbox', 'foreign_key' => 'user_id'),
                'outbox'            => array('model' => 'outbox', 'foreign_key' => 'user_id'),
                'invitations'       => array('model' => 'invitation', 'foreign_key' => 'user_id'),
                'user_educations'   => array('model' => 'user_education', 'foreign_key' => 'user_id'),
                'user_experiences'  => array('model' => 'user_experience', 'foreign_key' => 'user_id'),
                'contact_requests'  => array('model' => 'contact', 'foreign_key' => 'recipient_id')
	);
        
        protected $_has_one =  array(
                'user_additional'   => array('model' => 'user_additional', 'foreign_key' => 'user_id'),
                'user_profile'  => array('model' => 'user_profile', 'foreign_key' => 'user_id'),
                'desktop'  => array('model' => 'user_desktop', 'foreign_key' => 'user_id'),
                'notice'        => array('model' => 'notice', 'foreign_key' => 'user_id'),
                'user_privacy'  => array('model' => 'user_privacy', 'foreign_key' => 'user_id')
        );
    
        /**
        * Generates unieqe recovery token of given length.
        * @param int $length
        * @return string
        */
        public function uniqe_recovery_token($lenght = 32)
        {
            $token = Security::generate_token($lenght);
                    
            $is_uniqe = !DB::select()->from('users')->where('recovery_token','=',$token)->execute()->count();
                  
            if ($is_uniqe)
            {
                return $token;
            }
            else 
            {
                $this->uniqe_recovery_token($lenght);
            }
           
           
        }
        
} // End User Model